package com.article;

import com.article.model.ArticleInfo;
import com.article.model.ArticleType;
import com.article.service.ArticleInfoService;
import com.article.service.ArticleTypeService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * Created by Administrator on 2017/4/28.
 */
@Component
public class InitComponent implements ServletContextListener, ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext application = servletContextEvent.getServletContext();
        ArticleTypeService articleTypeService = (ArticleTypeService) applicationContext.getBean("articleTypeService");
        List<ArticleType> articleTypeList = articleTypeService.listCount();// 查询文章类别以及文章的数量
        application.setAttribute("articleTypeCount", articleTypeList);

        ArticleInfoService articleInfoService = (ArticleInfoService) applicationContext.getBean("articleInfoService");
        List<ArticleInfo> articleInfoList = articleInfoService.listCount();//按日期查询文章数量
        application.setAttribute("articleCount", articleInfoList);
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
